package Marco::Database;

use warnings;
use strict;

=head1 NAME

Marco::Database - The great new Marco::Database!

=head1 VERSION

Version $Rev: 15 $

=cut

use version 0.74;
our $VERSION = version->new(qw$Revision: 15 $);


=head1 SYNOPSIS

Quick summary of what the module does.

Perhaps a little code snippet.

    use Marco::Database;

    my $foo = Marco::Database->new();
    ...

=head1 METHODS

=head2 init

Extends the base class.  Sets up a profiler and some logging stuff

=cut

sub init {
	my $self = shift;

	$self->SUPER::init(@_);
	$self->{connect_logger} = Log::Log4perl::get_logger("Logger::Connect") unless defined $self->{connect_logger};
	$self->{DBH} = {} unless $self->{DBH};

	my $timer = new Library::Database::Profiler(0);
	$self->profiler($timer);
	$self->t_log( Log::Log4perl::get_logger('Marco::Database::Transaction') );
	return $self;
}


=head2 get_dbh

Gets a handle to the database

=cut


sub get_dbh {
	my $self=shift;
	
	return 	$self->{DBH};
}

=head2 release_dbh

Releases a dbh handle

=cut

sub release_dbh {
	my $self=shift;
	
	return;	
}

=head2 connect($db_host, $db_database, $db_user, $db_password)

Connect to the database

=cut
sub connect {
	my $self = shift;
	my $db_host       = shift;
	my $db_database   = shift;
	my $db_user       = shift;
	my $db_pass       = shift;
	
	unless ($db_host and $db_database and $db_user and $db_pass)
	{
		$self->logger->logcroak("No connection information passed in");
	}


	my $data_source = "DBI:Pg:dbname=$db_database;host=$db_host";
	
	my $dbh = DBI->connect($data_source, $db_user, $db_pass) ||
		$self->logger->logcroak("  (Database) Could not connect to database - '$data_source' !! $@\n");
	
	# Make it part of the object
	$self->{DBH} = $dbh;	
	
	return $db;
}

=head2 quote($sql_to_quote)

Escape your strings.  Calls the quote in DBI...

=cut
sub quote {
	my $self = shift;
	my $string = shift;

	return $self->{DBH}->quote($string);
}

=head2 mark_query()

Increment the number of queries generated

=cut
sub mark_query {
	my $self=shift;
	
	++$self->{_queries};	
	return $self->{_queries};	
}

=head2 mark_query()

Write the number of queries to the log

=cut
sub log_queries {
	my $self=shift;
	
	my $logger=$self->get_logger(1);
	$logger->debug("Queries", $db->{_queries});	
	return;
}


sub DESTROY {
	my $self=shift;
	$self->logger->trace("goodby cruel world");
	$self->SUPER::DESTROY if $self->can("SUPER::DESTROY");	
	$self->profiler(undef);

	
}

=head1 AUTHOR

Cory R. King, C<< <coryking at mozimedia.com> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-marco-database at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Marco-Database>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.




=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Marco::Database


You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Marco-Database>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Marco-Database>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Marco-Database>

=item * Search CPAN

L<http://search.cpan.org/dist/Marco-Database>

=back


=head1 ACKNOWLEDGEMENTS


=head1 COPYRIGHT & LICENSE

Copyright 2009 Cory R. King, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.


=cut

1; # End of Marco::Database
